package com.gzcss.gulimall.product.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.gzcss.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gzcss.gulimall.product.entity.AttrEntity;
import com.gzcss.gulimall.product.service.AttrAttrgroupRelationService;
import com.gzcss.gulimall.product.service.AttrService;
import com.gzcss.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzcss.common.utils.PageUtils;
import com.gzcss.common.utils.Query;

import com.gzcss.gulimall.product.dao.AttrGroupDao;
import com.gzcss.gulimall.product.entity.AttrGroupEntity;
import com.gzcss.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id = ? and (attr_group_name = ? or descript like %？%)
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.eq("attr_group_name", key).or().like("descript", key);
            });
        }

        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    //new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
                     queryWrapper);
            return new PageUtils(page);
        }else{
            queryWrapper.eq("catelog_id",catelogId);

            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper);
            return new PageUtils(page);
        }

    /**
     * 根据分类ID查出所有分组以及这些分组里面的属性
      * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithattrByCatelogId(Long catelogId) {
        //查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> collect = new ArrayList<>();
        if(attrGroupEntities != null && attrGroupEntities.size() >0 ){
            //查询所有属性
            collect = attrGroupEntities.stream().map(attrGroupEntity -> {
                AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
                BeanUtils.copyProperties(attrGroupEntity,attrsVo);
                //查找中间表找到分组对应的属性
                List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
                attrsVo.setAttrs(attrEntities);
                return attrsVo;
            }).collect(Collectors.toList());
        }

        return collect;
    }


}
